import javax.swing.JButton;

public class PassByReference {

	public static void main(String[] argv) {

		// test int
		ExtendedInt i = new ExtendedInt(5);

		// test array
		int[] list = new int[1];
		list[0] = 1;

		// test Swing component(normal object)
		JButton b = new JButton();
		b.setText("1");

		PassByReference p1 = new PassByReference(i);
		PassByReference p2 = new PassByReference(list);
		PassByReference p3 = new PassByReference(b);

		int i11 = 1;
		PassByReference p11 = new PassByReference(i11);
		System.out.println(i11 + "flasdf");

		// the reason why i doesn't change is, line 57 is really like i = new
		// int(111), so the address of i changed, but the value at original
		// address of i stays, to pass the pointer, wrap int to an object

		// a way to pass pointer to int
		PassByReference p4 = new PassByReference();
		p4.setValue(1);
		PassByReference p5 = new PassByReference(p4);

		System.out.println(i.i);
		System.out.println(p4.value);
		System.out.println(list[0]);
		System.out.println(b.getText());

	}

	public int value;

	/**
	 * in all, java is pass-by-value, but the value is a pointer to the passed
	 * object, so internal access to a parameter will change the parameter's
	 * value, the reason why type int doesn't change is assign value to a int is
	 * really like create a new object- i = 0 <--> i = new int(0)
	 */
	PassByReference() {
	}

	/**
	 * int is 'pass-by-value'
	 * 
	 * @param i
	 */
	PassByReference(ExtendedInt i) {

		// i = new Integer(111);
		i.setValue(111);
	}

	/**
	 * to pass the pointer of an int, wrap it up to object
	 * 
	 * @param i
	 */

	PassByReference(PassByReference p) {

		p.setValue(111);
	}

	/**
	 * array is 'pass-by-reference' (pass the pointer to the object)
	 * 
	 * @param i
	 */
	PassByReference(int[] i) {
		i[0] = 111;
	}

	PassByReference(int i) {
		i = 111;
		System.out.println(i + "dsd");
	}

	/**
	 * Swing component(normal object) is 'pass-by-reference' (pass the pointer
	 * to the object)
	 * 
	 * @param b
	 */
	PassByReference(JButton b) {
		b.setText("111");
	}

	public void setValue(int i) {
		value = i;
	}

}
